package slim3.demo.controller.test;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;



public class ReadXMLFile {
	
		
	public static void main(String[] argv) throws Exception{
	    
	    //get all
	    ReadXMLFile test = new ReadXMLFile();
        ArrayList<FlightBean> beans = test.getAll();
        
        for (int i=0; i<beans.size(); i++) {
            System.out.println(beans.get(i).toString());
        }
        
	    //get KNH
//	    ReadXMLFile_backup_original test = new ReadXMLFile_backup_original();
//        ArrayList<FlightBean> beans = test.getKNH();
//        
//        for (int i=0; i<beans.size(); i++) {
//            System.out.println(beans.get(i).toString());
//        }
	    
	    //get MFK and LZN
//	    ReadXMLFile_backup_original test = new ReadXMLFile_backup_original();
//        ArrayList<FlightBean> beans = test.getMFKandLZN();
//        
//        for (int i=0; i<beans.size(); i++) {
//            System.out.println(beans.get(i).toString());
//        }
        
	}
	
	
	
	public ArrayList<FlightBean> getKNH() throws Exception {
	    ArrayList<FlightBean> knh = new ArrayList<FlightBean>();
	    
	    ArrayList<FlightBean> beans = getAll();
	    FlightBean bean;
        
        for (int i = 0; i < beans.size(); i++) {

            if (beans.get(i).getDestination().equals("金門KNH")) {
                
                bean = new FlightBean();
                bean.setAirline(beans.get(i).getAirline());
                bean.setDestination("KNH");
                bean.setFlightNo(beans.get(i).getFlightNo());
                bean.setSchedTime(beans.get(i).getSchedTime());
                bean.setActualTime(beans.get(i).getActualTime());
                bean.setStatus(beans.get(i).getStatus());
                                
                knh.add(bean);
            }
            
        }
	    
	    return knh;
	}
	
	
	public ArrayList<FlightBean> getMFKandLZN() throws Exception {
        ArrayList<FlightBean> MFKandLZN = new ArrayList<FlightBean>();
        
        ArrayList<FlightBean> beans = getAll();
        FlightBean bean;
        
        for (int i = 0; i < beans.size(); i++) {

            if (beans.get(i).getDestination().equals("北竿MFK")) {
                
                bean = new FlightBean();
                bean.setAirline(beans.get(i).getAirline());
                bean.setDestination("MFK");
                bean.setFlightNo(beans.get(i).getFlightNo());
                bean.setSchedTime(beans.get(i).getSchedTime());
                bean.setActualTime(beans.get(i).getActualTime());
                bean.setStatus(beans.get(i).getStatus());
                                
                MFKandLZN.add(bean);
            }
            
            if (beans.get(i).getDestination().equals("南竿LZN")) {

                bean = new FlightBean();
                bean.setAirline(beans.get(i).getAirline());
                bean.setDestination("LZN");
                bean.setFlightNo(beans.get(i).getFlightNo());
                bean.setSchedTime(beans.get(i).getSchedTime());
                bean.setActualTime(beans.get(i).getActualTime());
                bean.setStatus(beans.get(i).getStatus());
                                
                MFKandLZN.add(bean);
            }
            
        }
        
        return MFKandLZN;
    }
	
	
	public ArrayList<FlightBean> getAll() throws Exception{

	    ArrayList<FlightBean> all = new ArrayList<FlightBean>();
	    FlightBean bean = null;
	    String html = "";
	    

		try {

		  
			/**
			 * read from URL
			 */
			URL tsa = new URL("http://www.tsa.gov.tw/CustomerSet/tsa/tp_FlightSchedule/RSS_news.asp?id={FE8E2C05-9D2C-482F-9919-4A3A3387E71A}&dfc=1");
						
			URLConnection urlc = tsa.openConnection();
			urlc.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
			urlc.setDoOutput(true);
			urlc.setDoInput(true);
			
//			BufferedReader in = new BufferedReader(
//	            new InputStreamReader(tsa.openStream()));
			BufferedReader in = new BufferedReader(new InputStreamReader(urlc.getInputStream()));

//	        String inputLine;
//	        while ((inputLine = in.readLine()) != null)
//	            System.out.println(inputLine);
//	        in.close();
  
    
			/**
			 * read from file
			 */
			//BufferedReader in = new BufferedReader(new FileReader("RSS.xml"));
  
  
			/**
			 * process code below
			 */
			String str;
			boolean finished = false;

				while ( ((str = in.readLine()) != null) && !finished) {
				    
					if (str.equals("<![CDATA[<table width=100% border=1 cellpadding=2 cellspacing=1><tr>")) {
					html = html + "<table width=100% border=1 cellpadding=2 cellspacing=1><tr>\n";
					
					 while ((str = in.readLine()) != null) {
						 
						 if (str.equals("</table>]]>")) {
							 html = html + "</table>";
							 finished = true;
							 break;
						 }
						 
						 
						 if (str.contains("<td align")) {
							 String a, b, c, d, e, f;
							 a = str;
							 b = in.readLine();
							 c = in.readLine();
							 d = in.readLine();
							 e = in.readLine();
							 f = in.readLine();
							 
							 bean = addFlightBean(a,b,c,d,e,f);
							 all.add(bean);
							 
							 html = html + a + "\n" + b + "\n" + c + "\n" + d + "\n" + e + "\n" + f + "\n";
							 
						 } else {
				
				    		 html = html + str + "\n";
						 }
				
						 
					 }
					 
				}
	
			//System.out.println(str);
			}
			
			in.close();
			
			//System.out.println(html);
		    
		    
		    
			} catch (IOException e) {
			    e.getStackTrace();
			}
	  
		return all;
			
	}
	  
	  
	  
	  
	  
	public FlightBean addFlightBean(String a, String b, String c, String d, String e, String f) {
		  
		  String t;
		  FlightBean bean = new FlightBean();
		  
		  //airline
		  //t = "<td align=\"center\" >華信<br>AE   </td>";
		  t = a;
//		  System.out.println(t);
		  t = t.substring(t.indexOf(">")+1, t.indexOf("</")).trim();
		  t = t.substring(0, 2) + t.substring(6, 8);
//		  System.out.println(t);
		  bean.setAirline(t);
		  
		  //destination
		  //t = "<td align=\"center\" >馬公<br>MZG</td>";
		  t = b;
//		  System.out.println(t);
		  t = t.substring(t.indexOf(">")+1, t.indexOf("</")).trim();
//        t = t.substring(t.indexOf("br>")+3, t.indexOf("</t")).trim();
		  t = t.substring(0, 2) + t.substring(6, 9);
//		  System.out.println(t);
		  bean.setDestination(t);
		  
		  //flight no
		  //t = "<td align=\"center\" >369</td>";
		  t = c;
//		  System.out.println(t);
//		  System.out.println(t.substring(t.indexOf(">")+1, t.indexOf("</")));
		  bean.setFlightNo(t.substring(t.indexOf(">")+1, t.indexOf("</")));
		  
		  //sched time
		  //t = "<td align=\"center\" >2010</td>";
		  t = d;
//		  System.out.println(t);
//		  System.out.println(t.substring(t.indexOf(">")+1, t.indexOf("</")));
		  bean.setSchedTime(t.substring(t.indexOf(">")+1, t.indexOf("</")));
		  
		  //actual time
		  //t = "<td align=\"center\" >2010</td>";
		  t = e;
//		  System.out.println(t);
//		  System.out.println(t.substring(t.indexOf(">")+1, t.indexOf("</")));
		  bean.setActualTime(t.substring(t.indexOf(">")+1, t.indexOf("</")));
		  
		  //status
		  //t = "<td align=\"center\" >準時On Time</td>";
		  t = f;
//		  System.out.println(t);
//		  System.out.println(t.substring(t.indexOf(">")+1, t.indexOf("</")));
		  bean.setStatus(t.substring(t.indexOf(">")+1, t.indexOf("</")));
		  
//		  System.out.println(bean);
		  

		  return bean;
		  
	}

}
